﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Utility.NodeGraphs;
using Utility.Math;
using UnityEditor;
using Utility.Inspector;

namespace Utility.BehaviourTree
{
	[NodeGraphEditor (typeof (VisualizerNodeGraph))]
	public class VisualizerNodeGraphEditor : TreeNodeGraphEditor
	{
		public override Rect GetCanvasRect (Rect rect, NodeGraph graph)
		{
			return rect.ShrinkLeft (220);
		}

		public override GenericMenu BuildContextMenu (NodeGraph graph)
		{
			GenericMenu menu = new GenericMenu ();
			AddMenuItem_FormatNodeGraph (menu, graph);
			menu.AddSeparator ("");
			menu.AddItem (new GUIContent ("Stop Monitor"), false,
				() =>
					{
						BTVisualizerClient.target = null;
						BTVisualizerClientWindow.StopMonitor ();
					});
			return menu;
		}

		Vector2 scrollpos;
		public override void DrawOverlayGUI (Rect rect, NodeGraph graph)
		{
			DataBoard data = ((graph as VisualizerNodeGraph).RootNode as VisualizerNode).btNode.DataBoard;

			Rect rect_region = rect.DockLeft (220);
			GUILayout.BeginArea (rect_region);

			using (ScrollViewScope.New (ref scrollpos))
			{
				using (EditorGUIWidthScope.NewLabelWidth (80))
				{
					//general info
					EditorGUILayout.LabelField ("Data Board", EditorStyles.boldLabel);
					EditorGUILayout.IntField ("updates", data.Updates);
					EditorGUILayout.FloatField ("time", data.Time);
					EditorGUILayout.FloatField ("delta time", data.DeltaTime);

					//int
					EditorGUILayout.Separator ();
					EditorGUILayout.LabelField ("int:", EditorStyles.boldLabel);
					foreach (var pair in data.IntKVPairs)
					{
						EditorGUILayout.IntField (pair.Key, pair.Value);
					}

					//float
					EditorGUILayout.Separator ();
					EditorGUILayout.LabelField ("float:", EditorStyles.boldLabel);
					foreach (var pair in data.FloatKVPairs)
					{
						EditorGUILayout.FloatField (pair.Key, pair.Value);
					}

					//bool
					EditorGUILayout.Separator ();
					EditorGUILayout.LabelField ("bool:", EditorStyles.boldLabel);
					foreach (var pair in data.BoolKVPairs)
					{
						EditorGUILayout.TextField (pair.Key, pair.Value.ToString ());
					}

					//vec3
					EditorGUILayout.Separator ();
					EditorGUILayout.LabelField ("Vector3:", EditorStyles.boldLabel);
					foreach (var pair in data.Vec3KVPairs)
					{
						EditorGUILayout.Vector3Field (pair.Key, pair.Value);
					}

					//object
					EditorGUILayout.Separator ();
					EditorGUILayout.LabelField ("object:", EditorStyles.boldLabel);
					foreach (var pair in data.ObjectKVPairs)
					{
						EditorGUILayout.TextField (pair.Key, pair.Value.ToString ());
					}
				}
			}
			GUILayout.EndArea ();
		}
	}
}